

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using JTC.MiniCommerce.Core;
using JTC.MiniCommerce.Store;
using SubSonic.Utilities;

namespace JTC.MiniCommerce.Web {
  public partial class search : System.Web.UI.Page {

    #region Member Variables

    private PagedDataSource pagedDataSource = new PagedDataSource();
    private string searchTerms = string.Empty;

    #endregion

    #region Page Events

    protected void Page_Load(object sender, EventArgs e) {
      try {
        searchTerms = HttpUtility.UrlDecode(Utility.GetParameter("searchTerms"));
        string p = Utility.GetParameter("p");

        if (!string.IsNullOrEmpty(searchTerms)) {
          if (string.IsNullOrEmpty(p)) {
            if (Master.SiteSettings.CollectSearchTerms) {
              BrowsingLog browsingLog = new BrowsingLog();
              browsingLog.BrowsingBehaviorId = (int)BrowsingBehaviour.Search;
              browsingLog.SearchTerms = searchTerms;
              browsingLog.UserName = WebUtility.GetUserName();
              browsingLog.Url = Request.Url.ToString();
              browsingLog.SessionId = WebUtility.SessionId();
              browsingLog.Save("System");
            }
          }
          LoadProducts();
        }
      }
      catch (Exception ex) {
        Logger.Error(typeof(search).Name + ".Page_Load", ex);
        throw;
      }
    }

    #endregion

    #region Methods

    #region Private

    /// <summary>
    /// Loads the products.
    /// </summary>
    private void LoadProducts() {
      pagedDataSource.AllowPaging = true;
      pagedDataSource.PageSize = Master.SiteSettings.CatalogItems;
      int currentPageIndex = 0;
      int.TryParse(Utility.GetParameter("p"), out currentPageIndex);
      pagedDataSource.CurrentPageIndex = currentPageIndex;

      pagedDataSource.DataSource = new ProductController().SearchProducts(searchTerms);

      topPager.PagedDataSource = pagedDataSource;
      bottomPager.PagedDataSource = pagedDataSource;
      catalogList.PagedDataSource = pagedDataSource;
    }

    #endregion

    #endregion

  }
}
